/******************************************************************************
 *File   : trace.h
 *Author : Lan Hu
 *Email	: Hi_Blue@126.com
 *Notes	:
 *Licence: 
 *Created: 2008-07-10
 *******************************************************************************/
#ifndef _MYLIB_INCLUDE__TRACE_H__20080710_
#define _MYLIB_INCLUDE__TRACE_H__20080710_

#include <stdarg.h>
#include "public.h"
#include "Time.h"

M_BEGIN_NAMESPACE(mylib)

#define TRACE_BUFFER_LEN 1024

#if (M_DEBUG_TRACE)

#if ( M_OS_TYPE == M_LINUX_OS )

#include <sys/param.h>

inline void debug_print(FILE * fp, const char * filename, const int line, const char * funcname, char *fmt, ...)
{
	char buf[1024];
	Time::T_TM now;
	Time::T_TIME_T t = Time::GetCurrTime();
	Time::GetTime(t,&now);
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(fp,"[%04d-%02d-%02d %02d:%02d:%02d]  ", now.tm_year, now.tm_mon , now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
	fprintf(fp, "%-80s[ * %s * %s:%d ]\n",buf, funcname,filename, line);
}

inline void error_print(FILE * fp, const char * filename, const int line, const char * funcname, char *fmt, ...)
{
	char buf[1024];
	Time::T_TM now;
	Time::T_TIME_T t = Time::GetCurrTime();
	Time::GetTime(t,&now);
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(fp,"[%04d-%02d-%02d %02d:%02d:%02d]  ", now.tm_year, now.tm_mon , now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
	fprintf(fp, "ERROR:%-80s[ * %s * %s:%d ]\n",buf, funcname,filename, line);
	exit(-1);
}

inline void warning_print(FILE * fp, const char * filename, const int line, const char * funcname, char *fmt, ...)
{
	char buf[1024];
	Time::T_TM now;
	Time::T_TIME_T t = Time::GetCurrTime();
	Time::GetTime(t,&now);
	va_list ap;
	va_start(ap, fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(fp,"[%04d-%02d-%02d %02d:%02d:%02d]  ", now.tm_year, now.tm_mon , now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec);
	fprintf(fp, "WARNING:%-80s[ * %s * %s:%d ]\n",buf, funcname,filename, line);
}


#define M_FTRACE(fp, x...)  debug_print(fp,__FILE__,__LINE__,__FUNCTION__,##x);

#if(M_TREACTOFILE)

#define M_TRACE(x...)													\
	FILE * pFile = fopen("Trace.log","a+");								\
	assert(pFile);														\
	debug_print(pFile,__FILE__,__LINE__,__FUNCTION__,##x);				\
	fclose(pFile);

#define M_ERROR(x...)													\
	FILE * pFile = fopen("Trace.log","a+");								\
	assert(pFile);														\
	error_print(pFile,__FILE__,__LINE__,__FUNCTION__,##x);				\
	fclose(pFile);

#define M_WARNING(x...)													\
	FILE * pFile = fopen("Trace.log","a+");								\
	assert(pFile);														\
	waring_print(pFile,__FILE__,__LINE__,__FUNCTION__,##x);				\
	fclose(pFile);

#else

#define M_TRACE(x...)  debug_print(stdout,__FILE__,__LINE__,__FUNCTION__,##x);
#define M_ERROR(x...)  error_print(stdout,__FILE__,__LINE__,__FUNCTION__,##x);
#define M_WARNING(x...) warning_print(stdout,__FILE__,__LINE__,__FUNCTION__,##x);

#endif

#else	// WIN32

inline void M_TRACE(char *fmt, ...)
{
// 	va_list args;
// 	va_start(args,fmt);
// 	vprintf(fmt,args);
// 	va_end(args);
// 
// 	fflush(stdout);
	Time t;
	char buf[TRACE_BUFFER_LEN];
	va_list ap;
	va_start(ap,fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(stdout,"[%s] %s\n",t.getTimeStr().c_str(),buf);
 	fflush(stdout);
}

inline void M_ERROR(char *fmt, ...)
{
	Time t;
	char buf[TRACE_BUFFER_LEN];
	va_list ap;
	va_start(ap,fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(stdout,"[%s] ERROR:%s\n",t.getTimeStr().c_str(),buf);
	fflush(stdout);
	exit(-1);
}

inline void M_WARNING(char *fmt, ...)
{
	Time t;
	char buf[TRACE_BUFFER_LEN];
	va_list ap;
	va_start(ap,fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(stdout,"[%s] WARNING:%s\n",t.getTimeStr().c_str(),buf);
	fflush(stdout);
}

inline void M_FTRACE(FILE * fp,char *fmt, ...)
{
	Time t;
	char buf[TRACE_BUFFER_LEN];
	va_list ap;
	va_start(ap,fmt);
	vsnprintf(buf,TRACE_BUFFER_LEN,fmt,ap);
	va_end(ap);
	fprintf(fp,"[%s] %s\n",t.getTimeStr().c_str(),buf);
}


#endif	//WIN32


#else	//M_DEBUG_TRACE

#if ( M_OS_TYPE == M_LINUX_OS )	//LINUX

	#define M_TRACE(x...) 
	#define M_ERROR(x...) 
	#define M_WARNING(x...) 
	#define M_FTRACE(fp, x...) 

#else			//WIN32

	inline void M_TRACE(char *fmt, ...){}
	inline void M_ERROR(char *fmt, ...){}
	inline void M_WARNING(char *fmt, ...){}
	inline void M_FTRACE(FILE * fp,char *fmt, ...){}

#endif	//WIN32

#endif	//M_DEBUG_TRACE

M_END_NAMESPACE(mylib)

#endif  // _MYLIB_INCLUDE__TRACE_H__20080710_

